
package com.heimali.happyreader.core.net;

import android.text.TextUtils;

import com.heimali.happyreader.HappyReaderApplication;
import com.heimali.happyreader.config.ErrorCode;
import com.heimali.happyreader.core.bean.ManagedRuntimeException;
import com.heimali.happyreader.core.bean.User;
import com.heimali.happyreader.debug.Console;
import com.heimali.happyreader.utils.NetCheckUtils;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

public class Request {
    public static final String TAG = "Request";

    private static final String DEFAULT_CHARSET = "GBK";
    private static final String HEADER_PARAM_HOST = "HOST";

    static public String get(URI uri) {
        return get(uri, DEFAULT_CHARSET);
    }

    /**
     * 绑定host的get请求
     * @param uri
     * @param hostname 
     * @return 
     */
    static public String getWithHost(URI uri, String hostname) {
        return get(uri, null, hostname);
    }

    static public String get(URI uri, String charset) {
        return get(uri, charset, null);
    }

    /**
     * get请求
     * @param uri ： 如 http://173.83.127.172/index.html  此IP必须是服务器IP，不能是LVS的IP
     * @param charset : 页面编码， 默认为GBK
     * @param hostname : 要访问的域名 www.heimali.com
     * @return 请求体内容
     */
    static public String get(URI uri, String charset, String hostname) {
        boolean hasNet = NetCheckUtils.isNetworkAvailable();
        if (!hasNet) {
            throw new ManagedRuntimeException(ErrorCode.NET_UNAVAILABLE, "network unavailable");
        }
        
        String strResult = "request error";
        HttpGet request = new HttpGet(uri);
        if (TextUtils.isEmpty(charset)) {
            charset = DEFAULT_CHARSET;
        }
        if (!TextUtils.isEmpty(hostname)) {
            request.addHeader(HEADER_PARAM_HOST, hostname);
        }
        
        User userInfo = HappyReaderApplication.curUserInfo;
        if (null != userInfo) {
            request.addHeader("Cookie", "id="+userInfo.getUid()+";token="+userInfo.getToken());
        }
        
        try {
            HttpClient client = new DefaultHttpClient();
//            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
//            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
//            Console.log(TAG, "get uri", request.getURI());
            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                strResult = EntityUtils.toString(response.getEntity(), charset);
//                Console.log(TAG, "result", strResult);
            }
        } catch (ClientProtocolException e) {
            e.getStackTrace();
            Console.log(TAG, "ClientProtocolException", e.getMessage());
            throw new ManagedRuntimeException();
        } catch (IOException e) {
            e.getStackTrace();
            Console.log(TAG, "IOException", e.getMessage());
            throw new ManagedRuntimeException();
        }
        return strResult;
    }

    static public String postWithHost(URI uri, String hostname) {
        return post(uri, null, hostname);
    }

    static public String post(URI uri, String charset) {
        return post(uri, charset, null);
    }

    static public String post(URI uri, String charset, String hostname) {
        String strResult = "request error";
        HttpPost request = new HttpPost(uri);
        if (TextUtils.isEmpty(charset)) {
            charset = DEFAULT_CHARSET;
        }
        if (!TextUtils.isEmpty(hostname)) {
            request.addHeader(HEADER_PARAM_HOST, hostname);
        }

        try {
            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                strResult = EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return strResult;
    }

}
